package com.hjimi.ski.presenter;

import android.util.Log;

import com.hjimi.ski.bean.DeviceSystemBean;
import com.hjimi.ski.callback.BaseCallback;
import com.hjimi.ski.config.UrlConfig;
import com.hjimi.ski.iView.DeviceSystemIView;
import com.hjimi.ski.model.base.DataModel;
import com.hjimi.ski.model.base.ModelToken;
import com.hjimi.ski.utils.JsonParseUtil;
import com.hjimi.ski.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created By 孙岩
 * on 2023/7/31
 */
public class DeviceSystemPresenter extends BasePresenter<DeviceSystemIView>{
    @SuppressWarnings("unchecked")
    public void getDeviceSystem(String deviceId){
        if(isViewAttached()){
            String url = UrlConfig.deviceSystem + deviceId;
            DataModel.request(ModelToken.GET_REQUEST)
                    .url(url)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                Log.d("sunyan","收到的系统配置信息================"+data);
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    JSONObject bgmConfigObj = new JSONObject(object.getString("bgm_config"));
                                    DeviceSystemBean.DataBean.BgmConfigBean bgmConfigBean = new DeviceSystemBean.DataBean.BgmConfigBean(
                                            JsonParseUtil.getBoolean(bgmConfigObj,"is_open"),
                                            JsonParseUtil.getInt(bgmConfigObj,"bgm_id"),
                                            JsonParseUtil.getStr(bgmConfigObj,"bgm_url"),
                                            JsonParseUtil.getBoolean(bgmConfigObj,"is_loop")
                                    );

                                    JSONObject turnConfigObj = new JSONObject(object.getString("turn_config"));
                                    DeviceSystemBean.DataBean.TurnConfigBean turnConfigBean = new DeviceSystemBean.DataBean.TurnConfigBean(
                                            JsonParseUtil.getBoolean(turnConfigObj,"is_open"),
                                            JsonParseUtil.getStr(turnConfigObj,"start_time"),
                                            JsonParseUtil.getStr(turnConfigObj,"end_time"),
                                            JsonParseUtil.getStr(turnConfigObj,"weeks")
                                    );
                                    DeviceSystemBean.DataBean bean = new DeviceSystemBean.DataBean(
                                            JsonParseUtil.getInt(object,"id"),
                                            JsonParseUtil.getStr(object,"ip"),
                                            JsonParseUtil.getInt(object,"is_carousel"),
                                            JsonParseUtil.getInt(object,"is_gesture"),
                                            JsonParseUtil.getInt(object,"carousel_seconds"),
                                            bgmConfigBean,
                                            turnConfigBean
                                    );
                                    getView().showDeviceSystem(bean);
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
